package com.xilihui.xlh.business.activitys.store;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.HotKeyEntity;
import com.xilihui.xlh.business.requests.StoreRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.component.recyclerview.LinearLayoutColorDivider;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.feezu.liuli.timeselector.Utils.TextUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/5/25.
 */

public class StoreHisSearchActivity extends BaseCompatActivity{
    
    public static final String search = "search";
    
    @BindView(R.id.et_toolbar_title)
    EditText et_title;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.recyclerview_rot)
    RecyclerView recyclerView_rot;
    
    String keyword = "";
    
    String key = "";
    String[] keytag;
    ArrayList<String> keydata = new ArrayList<>();
    BaseAdapter<String> baseAdapter;
    
    ArrayList<HotKeyEntity.DataBean> hotData = new ArrayList<>();
    BaseAdapter<HotKeyEntity.DataBean> hotAdapter;
    
    @Override
    public int getLayout() {
        return R.layout.activity_store_his_search;
    }
    
    @OnClick(R.id.iv_toolbar_left)
    public void back(){
        finish(); 
    }

    @Override
    public String initTitle() {
        return null;
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {

        key = (String)SPUtil.get(this,SPUtil.KEYWORD,"");
        if(!TextUtil.isEmpty(key)){
            keytag = key.split(";");
            for(int i=0;i<keytag.length;i++){
                keydata.add(keytag[i]);
            }
        }
        

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.addItemDecoration(new LinearLayoutColorDivider(getResources(),android.R.color.white,
                R.dimen.divider_space,LinearLayout.VERTICAL));
        
        GridLayoutManager gridLayoutManager1 = new GridLayoutManager(this,3);
        recyclerView_rot.setLayoutManager(gridLayoutManager1);
        recyclerView_rot.addItemDecoration(new LinearLayoutColorDivider(getResources(),R.color.white,
                R.dimen.divider_space,LinearLayout.VERTICAL));

        baseAdapter = new BaseAdapter<String>(this,keydata) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_search_keyword;
            }

            @Override
            protected void getView(BaseViewHolder holder, final String entity, int position) {
                holder.setText(R.id.tv_keyword,entity);
                
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        YEventBuses.post(new YEventBuses.Event(search).setParams(entity));
                        finish();
                    }
                });
                
            }
        };
        
        baseAdapter.setAnimationsLocked(true);
        recyclerView.setAdapter(baseAdapter);
        
        et_title.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                keyword = et_title.getText().toString().trim();
                
                if(actionId == EditorInfo.IME_ACTION_SEARCH){
                    if(!TextUtil.isEmpty(keyword)){
                        boolean isAdd = true;//是否有相同的历史记录 
                        for (int i=0;i<keydata.size();i++){
                            if(keyword.equals(keydata.get(i))){
                                isAdd = false;
                            }
                        }
                        
                        if(isAdd){
                            key = key + keyword + ";";
                            SPUtil.put(StoreHisSearchActivity.this,SPUtil.KEYWORD,key);
                        }
                        
                        YEventBuses.post(new YEventBuses.Event(search).setParams(keyword));
                        finish();
                        return false;
                    }
                    return true;
                }
                return false;
            }
        });
        
        hotAdapter = new BaseAdapter<HotKeyEntity.DataBean>(this,hotData) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_search_keyword;
            }

            @Override
            protected void getView(BaseViewHolder holder, final HotKeyEntity.DataBean entity, int position) {
                holder.setText(R.id.tv_keyword,entity.getKeywords());
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        YEventBuses.post(new YEventBuses.Event(search).setParams(entity.getKeywords()));
                        finish();
                    }
                });
            }
        };
        
        recyclerView_rot.setAdapter(hotAdapter);
        hotAdapter.setAnimationsLocked(true);
        
    }
    

    @Override
    public void initData() {
        getData();
    }
    
    @OnClick(R.id.iv_delete)
    public void delete(){
        SPUtil.remove(StoreHisSearchActivity.this,SPUtil.KEYWORD);
        keydata.clear();
        baseAdapter.setList(keydata);
    }
    
    public void getData(){
        StoreRequest.keyWords()
                .compose(DoTransform.<HotKeyEntity>applyScheduler(this,true))
                .compose(this.<HotKeyEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<HotKeyEntity>(this) {
                    @Override
                    protected void onSuccess(HotKeyEntity entity) {
                        hotData.clear();
                        hotData.addAll(entity.getData());
                        hotAdapter.setList(hotData);
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
    }
    
    
}
